package com.study.clickhouse.service.impl;

import com.study.clickhouse.mapper.VisitStatsMapper;
import com.study.clickhouse.model.VisitStatsDO;
import com.study.clickhouse.request.PageRequest;
import com.study.clickhouse.request.SelectRegionCityRequest;
import com.study.clickhouse.service.VisitStatsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class VisitStatsServiceImpl implements VisitStatsService {

    @Autowired(required = false)
    private VisitStatsMapper visitStatsMapper;
    /**
     * 分页查询
     * @param pageRequest
     * @return
     */
    @Override
    public Map<String, Object> selectPageClickhouse(PageRequest pageRequest) {

        // 当前页数
        Integer currentPage = pageRequest.getCurrentPage();
        // 每页数据量
        Integer size = pageRequest.getSize();

        // 查询数据得count
        Long count = visitStatsMapper.selectPageClickhouseCount(pageRequest);

        // 计算数据起始条数
        Integer from = (currentPage - 1) * size;
        List<VisitStatsDO> visitStatsDOList = visitStatsMapper.selectPageClickhouse(pageRequest.getProductId(), from, size);

        Map<String, Object> res = new HashMap<>();
        res.put("total", count);
        res.put("data", visitStatsDOList);
        res.put("current_page", currentPage);

        return res;

    }

    /**
     * 根据区域，城市查询
     * @param selectRegionCityRequest
     * @return
     */
    @Override
    public List<VisitStatsDO> selectByRegionCity(SelectRegionCityRequest selectRegionCityRequest) {

        List<VisitStatsDO> visitStatsDOList = new ArrayList<>();
        if(selectRegionCityRequest.getType().equalsIgnoreCase("region")){
            // 按照区域统计
            visitStatsDOList = visitStatsMapper.selectByRegionCity(selectRegionCityRequest.getProductId(), selectRegionCityRequest.getStartTime(), selectRegionCityRequest.getEndTime());
        }else if(selectRegionCityRequest.getType().equalsIgnoreCase("day")){
            // 按照天统计
            visitStatsDOList = visitStatsMapper.selectByDay(selectRegionCityRequest.getProductId(), selectRegionCityRequest.getStartTime(), selectRegionCityRequest.getEndTime());
        }
        return visitStatsDOList;
    }
}
